<?php

use App\Models\User;
use App\Models\Admin;
use App\Models\Robot;
use App\Utils\FundType;
use App\Models\UserFund;
use Endroid\QrCode\QrCode;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use Intervention\Image\Facades\Image;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});

Route::get('pay-success', function () {
    $url = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'] . '/mobile/paySuccess.html';
    return redirect($url);
});

Route::post('getSn', 'Api\AlipayController@getSn');

Route::get('bind-wechat', 'Api\CloundPayController@bindWechat');


Route::get('test', function () {
    // $arr = range(1, 11000);
    $users = User::get();
    // $users = range(10000, 10100);
    // foreach ($users as $user) {
    //     Redis::srem('robot' . $user->id, $arr);
    //     Redis::srem('direct_robot' . $user->id, $arr);
    //     Redis::srem('indirect_robot' . $user->id, $arr);
    //     Redis::srem('team_robot' . $user->id, $arr);
    //     Redis::srem('team_robot_total' . $user->id, $arr);
    //     Redis::srem('direct_user' . $user->id, $arr);
    //     Redis::srem('indirect_user' . $user->id, $arr);
    //     Redis::srem('team_user' . $user->id, $arr);
    // }
    // $zarr = range(0, 20000);

    // foreach($zarr as $arr) {
    //     Redis::zrem('robot', $arr);  
    //     Redis::zrem('direct_robot', $arr);  
    //     Redis::zrem('indirect_robot', $arr);  
    //     Redis::zrem('team_robot_total', $arr);  
    //     Redis::zrem('direct_user', $arr);  
    //     Redis::zrem('indirect_user', $arr);  
    //     Redis::zrem('team_user', $arr);  
    // }
    return 'success';
});

Route::get('test2', function () {
    // $id = request()->id;
    // $robotCount = Redis::smembers('robot' . $id);
    // var_dump(count($robotCount));
    // array_pop($robotCount);
    // var_dump(count($robotCount));
    // Robot::where('user_id', $id)->destroy($robotCount);
    // $directRobotCount = Redis::smembers('direct_robot' . $id);
    // $indirectRobotCount = Redis::smembers('indirect_robot' . $id);
    // $teamRobotCount = Redis::smembers('team_robot' . $id);
    // $teamRobotTotalCount = Redis::smembers('team_robot_total' . $id);
    // return compact('robotCount', 'directRobotCount', 'indirectRobotCount', 'teamRobotCount', 'teamRobotTotalCount');
});


Route::get('test3', function () {
    // $funds = UserFund::where('type', FundType::ROBOT_INCOME)->where('created_at', '>',date('Y-m-d'))->limit(1000)->get();
    // DB::beginTransaction();
    // try {
    //     foreach($funds as $fund) {
    //         $user = User::find($fund->user_id);
    //         $user->amount = $user->amount -  floatval($fund->change_amount);
    //         $user->amount_total = $user->amount_total - floatval($fund->change_amount);
    //         $user->save();
    //         UserFund::destroy($fund->id);
    //     }
    //     DB::commit();
    // }catch(\Exception $e) {
    //     DB::rollback();
    // }
    
    // return count($funds);
});

Route::get('test4', function () {
   
    // // Redis::set('test', 1);
    // return Redis::get('test');
    // $users = User::get();
    // foreach($users as $user) {
    //     $user->joinTeam();
    // }
    // $robots = Robot::where('id', '>', request()->start)->where('id', '<=', request()->end)->get();

    // foreach($robots as $robot) {
    //     $robot->addTeamRobot();
    // }
    // return success($robots);
});
